#include "deepwidget.h"
#include "ui_deepwidget.h"
#include <QHeaderView>
#define MODELPATHS "../conf/default/deep.conf"
#include<QToolTip>

DeepWidget::DeepWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeepWidget)
{
    ui->setupUi(this);
    Init_Ui();
}

DeepWidget::~DeepWidget()
{
    delete ui;
}

void DeepWidget::SetCurWidget(int i){
    ui->stackedWidget->setCurrentIndex(i);
}

void DeepWidget::Init_Ui(){
    QTextStream& fstream=Yzty::GetTextSteam(MODELPATHS);
    QStringList items=fstream.readAll().split('#');
    items.removeFirst();
    auto&& pstruct=Yzty::ConfigParse_Struct(items);
    Init_Config(pstruct);
}


void DeepWidget::Init_Config(QMap<int,QVector<QPair<QString,QStringList>>>& pstruct){

    int n=pstruct.size();

    _models=QVector<QVector<QStandardItemModel*>>(n);
    _views=QVector<QVector<QTableView*>>(n);
    _plains=QVector<QVector<QPlainTextEdit*>>(n);

    for(int i=0;i<n;i++){

        QWidget* w=new QWidget(this);
        ui->stackedWidget->addWidget(w);
        QGridLayout* layout=new QGridLayout(w);
        QTabWidget* tabw=new QTabWidget(this);



        for(auto& temp:pstruct[i]){
            QWidget* tw=new QWidget(tabw);
            tabw->addTab(tw,temp.first);
            QStandardItemModel* model=new QStandardItemModel(tw);
            QTableView*         view =new QTableView(tw);
            QPlainTextEdit*     edit =new QPlainTextEdit(tw);
            view->setMouseTracking(true);
            connect(view, &QTableView::entered, this, &DeepWidget::doMouseTrackTip);


            view->setMinimumHeight(300);
            edit->setMinimumHeight(100);
            edit->setMaximumHeight(200);

            QVBoxLayout* layout=new QVBoxLayout(tw);
            layout->addWidget(view);
            layout->addSpacing(10);
            layout->addWidget(edit);

            view->setModel(model);
            _models[i].push_back(model);
            _views[i].push_back(view);
            _plains[i].push_back(edit);

            view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
            view->setEditTriggers(QAbstractItemView::NoEditTriggers);

            model->setHorizontalHeaderLabels(temp.second);

            tw->setLayout(layout);
        }

        layout->addWidget(tabw);
        w->setLayout(layout);
    }
}
void DeepWidget::doMouseTrackTip(QModelIndex index)
{
    QToolTip::showText(QCursor::pos(), index.data().toString());
}
